package spring.springbucks.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import spring.springentity.entity.table.CoffeeEntity;
import spring.springentity.repository.CoffeeRepository;

@Slf4j
@RestController
@RequestMapping("/TestRest")
public class TestRestController {

  /**
   * XML格式返回
   * @return
   */
  @Autowired
  private CoffeeRepository coffeeRepository;

  @GetMapping(value = "/xml/{name}", produces = MediaType.APPLICATION_XML_VALUE)//  consumes = MediaType.APPLICATION_JSON_VALUE,
  public ResponseEntity getXml(@PathVariable String name) {
    return new ResponseEntity(coffeeRepository.getOne(new QueryWrapper<CoffeeEntity>().eq("NAME", name)), HttpStatus.OK);
  }

  /**
   * JSON格式返回
   * @return
   */
  @GetMapping(value = "/json/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity getJson(@PathVariable String name) {
    return new ResponseEntity(coffeeRepository.getOne(new QueryWrapper<CoffeeEntity>().eq("NAME", name)), HttpStatus.OK);
  }

  @PostMapping(value = "/postxml", produces = MediaType.APPLICATION_XML_VALUE)//  consumes = MediaType.APPLICATION_JSON_VALUE,
  public ResponseEntity postXml(@RequestBody CoffeeEntity coffee) {
    return new ResponseEntity(coffeeRepository.getOne(new QueryWrapper<CoffeeEntity>().eq("NAME", coffee.getName())), HttpStatus.OK);
  }

  /**
   * JSON格式返回
   * @return
   */
  @PostMapping(value = "/postjson", produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity postJson(@RequestBody CoffeeEntity coffee) {
    return new ResponseEntity(coffeeRepository.getOne(new QueryWrapper<CoffeeEntity>().eq("NAME", coffee.getName())), HttpStatus.OK);
  }

}
